package com.derive.education_service.web.notice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.derive.education_service.common.global.Paper;
import com.derive.education_service.common.global.ResponseMessage;
import com.derive.education_service.entity.Notice;
import com.derive.education_service.entity.Platform;
import com.derive.education_service.mapper.NoticeMapper;
import com.derive.education_service.utils.MyFileUtils;
import com.derive.education_service.web.notice.bean.dto.NoticeAddDto;
import com.derive.education_service.web.notice.bean.dto.NoticeUpDto;
import com.derive.education_service.web.notice.bean.dto.QueryParam;
import com.derive.education_service.web.notice.service.NoticeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

@Service
@Slf4j
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private MyFileUtils myFileUtils;

    @Autowired
    private NoticeMapper noticeMapper;

    /**
     * 发布通告 -- 有上传文件就提交文件
     * @param file
     * @param noticeAddDto
     * @return
     */
    @Override
    public ResponseMessage addNotice(MultipartFile file, NoticeAddDto noticeAddDto) {
        // 数据填充
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeAddDto, notice);
        // 文件上传
        if (file != null && !file.isEmpty()) {
            // 上传文件
            try {
                ResponseMessage r = myFileUtils.uploadFile(file, "notice");
                if (r.getStatus() == 0) {
                    // 上传文件成功，添加地址
                    notice.setNUrl(String.valueOf(r.getData()));
                } else {
                    // 失败
                    return ResponseMessage.createByErrorMessage("文件上传失败{[addNotice]}");
                }
            } catch (IOException e) {
                log.error("文件上传异常{[addNotice]}");
                e.printStackTrace();
                return ResponseMessage.createByErrorMessage("文件上传异常{[addNotice]}");
            }
        }
        // 没有文件就正常走下去
        notice.setCreateTime(new Date());
        notice.setUpdateTime(new Date());

        // 新增
        int row = noticeMapper.insert(notice);
        return row <= 0 ? ResponseMessage.createByErrorMessage("新增通告异常")
                : ResponseMessage.createBySuccessMessage("通告新增成功");
    }

    /**
     * 编辑通告
     * @param file
     * @param noticeUpDto
     * @return
     */
    @Override
    public ResponseMessage upNotice(MultipartFile file, NoticeUpDto noticeUpDto) {
        // 数据填充
        Notice notice = new Notice();
        BeanUtils.copyProperties(noticeUpDto, notice);
        // 判断是否更新了文件
        if (file != null && !file.isEmpty()) {
            // 更新文件
            String url = noticeMapper.selectById(noticeUpDto.getNoticeId()).getNUrl();
            // 删除原本的
            // 删除资源
            ResponseMessage r = myFileUtils.delFile(url);
            if (r.getStatus() == 1) {
                // 失败退出
                log.error("删除通告资源异常{[delNotice]}");
                return r;
            }
            // 上传新文件
            try {
                ResponseMessage ar = myFileUtils.uploadFile(file, "notice");
                if (ar.getStatus() == 0) {
                    // 上传文件成功，更新地址
                    notice.setNUrl(String.valueOf(ar.getData()));
                } else {
                    // 失败
                    return ResponseMessage.createByErrorMessage("文件上传失败{[upNotice]}");
                }
            } catch (IOException e) {
                log.error("文件上传异常{[upNotice]}");
                e.printStackTrace();
                return ResponseMessage.createByErrorMessage("文件上传异常{[upNotice]}");
            }
        }
        // 更新数据
        notice.setUpdateTime(new Date());
        int row = noticeMapper.updateById(notice);
        return row <= 0 ? ResponseMessage.createByErrorMessage("更新通告异常")
                : ResponseMessage.createBySuccessMessage("通告更新成功");
    }

    /**
     * 删除通告 -- 有资源删除资源
     * @param noticeId
     * @return
     */
    @Override
    public ResponseMessage delNotice(int noticeId) {
        // 查询是否有资源
        String url = noticeMapper.selectById(noticeId).getNUrl();
        if (url != null && !url.isEmpty()) {
            // 删除资源
            ResponseMessage r = myFileUtils.delFile(url);
            if (r.getStatus() == 1) {
                // 失败退出
                log.error("删除通告资源异常{[delNotice]}");
                return r;
            }
        }
        // 删除信息
        int row = noticeMapper.deleteById(noticeId);
        return row <= 0 ? ResponseMessage.createByErrorMessage("删除通告信息异常")
                : ResponseMessage.createBySuccess("删除成功");
    }

    /**
     * 查询通告详情
     * @param noticeId
     * @return
     */
    @Override
    public ResponseMessage getNoticeById(int noticeId) {
        Notice notice = noticeMapper.selectById(noticeId);
        return notice == null ? ResponseMessage.createByErrorMessage("通告不存在")
                : ResponseMessage.createBySuccess(notice);
    }

    /**
     * 检索通告信息 -- 分页查询 -- 条件查询
     * @param queryParam
     * @return
     */
    @Override
    public Paper<Notice> getNoticeList(QueryParam queryParam) {
        // 封装分页
        Page<Notice> page = new Page<>();
        page.setCurrent(queryParam.getCurrent());
        page.setSize(queryParam.getSize());

        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        if (queryParam.getNTitle() != null && !queryParam.getNTitle().isEmpty()) {
            queryWrapper.like("n_title", queryParam.getNTitle());
        }

        Page<Notice> noticePage = noticeMapper.selectPage(page, queryWrapper);

        // 数据封装返回
        Paper<Notice> noticePaper = new Paper<>();
        noticePaper.setCurrent(noticePage.getCurrent());
        noticePaper.setSize(noticePage.getSize());
        noticePaper.setPages(noticePage.getPages());
        noticePaper.setTotal(noticePage.getTotal());
        noticePaper.setData(noticePage.getRecords());

        return noticePaper;
    }
}
